﻿using System;
using System.Windows.Forms;

namespace YAMM.PlugInManagement.Menu.ToolBar
{
    using PlugIn;

    class ToolStripItemCollection : System.Windows.Forms.ToolStripItemCollection, IToolStripItemCollection
    {
        ToolStrip owner;

        public ToolStripItemCollection(ToolStrip owner, ToolStripItem[] value)
            : base(owner, value)
        {
            this.owner = owner;
        }

        #region IToolStripItemCollection Members

        #region Add*

        #region AddButton
        public IToolStripButton AddButton(Guid identifier, string caption, EventHandler clickHandler)
        {
            ToolStripButton btn = new ToolStripButton();
            btn.Text = caption;
            btn.Identifier = identifier;
            btn.Click += clickHandler;
            btn.Owner = owner;
            btn.DisplayStyle = ToolStripItemDisplayStyle.Text;
            base.Add(btn);
            return btn;
        }

        public IToolStripButton AddButton(Guid identifier, string caption, EventHandler clickHandler, System.Windows.Forms.Keys shortcutKey)
        {
            ToolStripButton btn = (ToolStripButton)AddButton(identifier, caption, clickHandler);            
            btn.DisplayStyle = ToolStripItemDisplayStyle.Text;
            btn.ShortcutKeys = shortcutKey;
            return btn;
        }

        public IToolStripButton AddButton(Guid identifier, string caption, EventHandler clickHandler, System.Drawing.Image image)
        {
            ToolStripButton btn = (ToolStripButton)AddButton(identifier, caption, clickHandler);
            btn.Image = image;
            btn.DisplayStyle = ToolStripItemDisplayStyle.Image;
            return btn;            
        }

        public IToolStripButton AddButton(Guid identifier, string caption, EventHandler clickHandler, System.Drawing.Image image, System.Windows.Forms.Keys shortcutKey)
        {
            ToolStripButton btn = (ToolStripButton)AddButton(identifier, caption, clickHandler);
            btn.ShortcutKeys = shortcutKey;            
            return btn;
        }
        #endregion
        
        #region AddCheckableButton
        public IToolStripCheckableButton AddCheckableButton(Guid identifier, string caption, EventHandler clickHandler, bool isChecked)
        {
            ToolStripButton btn = new ToolStripButton();
            btn.Text = caption;
            btn.Identifier = identifier;
            btn.Click += clickHandler;
            btn.Owner = owner;
            btn.DisplayStyle = ToolStripItemDisplayStyle.Text;
            btn.CheckOnClick = true;
            btn.Checked = isChecked;
            base.Add(btn);
            return btn;
        }

        public IToolStripCheckableButton AddCheckableButton(Guid identifier, string caption, EventHandler clickHandler, bool isChecked, System.Windows.Forms.Keys shortcutKey)
        {
            ToolStripButton btn = (ToolStripButton)AddCheckableButton(identifier, caption, clickHandler, isChecked);
            btn.DisplayStyle = ToolStripItemDisplayStyle.Text;
            btn.ShortcutKeys = shortcutKey;
            return btn;
        }

        public IToolStripCheckableButton AddCheckableButton(Guid identifier, string caption, EventHandler clickHandler, bool isChecked, System.Drawing.Image image)
        {
            ToolStripButton btn = (ToolStripButton)AddCheckableButton(identifier, caption, clickHandler, isChecked);
            btn.Image = image;
            btn.DisplayStyle = ToolStripItemDisplayStyle.Image;
            return btn;
        }

        public IToolStripCheckableButton AddCheckableButton(Guid identifier, string caption, EventHandler clickHandler, bool isChecked, System.Windows.Forms.Keys shortcutKey, System.Drawing.Image image)
        {
            ToolStripButton btn = (ToolStripButton)AddCheckableButton(identifier, caption, clickHandler, isChecked);
            btn.ShortcutKeys = shortcutKey;
            return btn;
        }
        #endregion

        #region AddSubMenuDropDownButton
        public IToolStripDropDownButton AddSubMenuDropDownButton(Guid identifier, string caption, EventHandler clickHandler)
        {
            ToolStripDropDownButton btn = new ToolStripDropDownButton();
            btn.Text = caption;
            btn.Identifier = identifier;
            btn.Click += clickHandler;
            btn.Owner = owner;
            btn.DisplayStyle = ToolStripItemDisplayStyle.Text;
            base.Add(btn);
            return btn;
        }

        public IToolStripDropDownButton AddSubMenuDropDownButton(Guid identifier, string caption, EventHandler clickHandler, System.Drawing.Image image)
        {
            ToolStripDropDownButton btn = (ToolStripDropDownButton)AddSubMenuDropDownButton(identifier, caption, clickHandler);
            btn.Image = image;
            btn.DisplayStyle = ToolStripItemDisplayStyle.Image;            
            return btn;
        }
        #endregion

        #region AddSubMenuSplitButton
        public IToolStripSplitButton AddSubMenuSplitButton(Guid identifier, string caption, EventHandler clickHandler)
        {
            ToolStripSplitButton btn = new ToolStripSplitButton();
            btn.Text = caption;
            btn.Identifier = identifier;
            btn.Click += clickHandler;
            btn.Owner = owner;
            btn.DisplayStyle = ToolStripItemDisplayStyle.Text;
            base.Add(btn);
            return btn;
        }

        public IToolStripSplitButton AddSubMenuSplitButton(Guid identifier, string caption, EventHandler clickHandler, System.Drawing.Image image)
        {
            ToolStripSplitButton btn = (ToolStripSplitButton)AddSubMenuSplitButton(identifier, caption, clickHandler);
            btn.Image = image;
            btn.DisplayStyle = ToolStripItemDisplayStyle.Image;
            return btn;
        }
        #endregion

        #region AddSeparator
        public IToolStripSeparator AddSeparator(Guid identifier)
        {
            PlugInManagement.Menu.MenuBar.ToolStripSeparator separator = new PlugInManagement.Menu.MenuBar.ToolStripSeparator();
            separator.Owner = owner;
            separator.Identifier = identifier;
            base.Add(separator);
            return separator;
        }
        #endregion

        #region AddTextBox
        public IToolStripTextBox AddTextBox(Guid identifier, EventHandler clickHandler)
        {
            PlugInManagement.Menu.MenuBar.ToolStripTextBox textBox = new PlugInManagement.Menu.MenuBar.ToolStripTextBox();
            textBox.Identifier = identifier;
            textBox.Click += clickHandler;
            textBox.Owner = owner;
            base.Add(textBox);
            return textBox;
        }

        public IToolStripTextBox AddTextBox(Guid identifier, EventHandler clickHandler, string text)
        {
            PlugInManagement.Menu.MenuBar.ToolStripTextBox textBox = (PlugInManagement.Menu.MenuBar.ToolStripTextBox)AddTextBox(identifier, clickHandler);
            textBox.Text = text;
            return textBox;
        }
        #endregion

        #region AddComboBox
        public IToolStripComboBox AddComboBox(Guid identifier, EventHandler clickHandler)
        {
            PlugInManagement.Menu.MenuBar.ToolStripComboBox comboBox = new PlugInManagement.Menu.MenuBar.ToolStripComboBox();
            comboBox.Identifier = identifier;
            comboBox.Click += clickHandler;
            comboBox.Owner = owner;
            base.Add(comboBox);
            return comboBox;
        }
        #endregion
        #endregion

        public bool Contains(Guid identifier)
        {
            bool retval = false;
            for (int i = 0; i < Count; i++)
                if (identifier == this[i].Identifier)
                {
                    retval = true;
                    break;
                }
            return retval;
        }

        public void Remove(Guid identifier)
        {
            try
            {
                System.Windows.Forms.ToolStripItem item = (System.Windows.Forms.ToolStripItem)this[identifier];
                item.Dispose();
                base.Remove(item);
            }
            catch (ArgumentException)
            {
            }
        }

        public IToolStripItem this[Guid identifier]
        {
            get
            {
                for (int i = 0; i < Count; i++)
                    if (this[i].Identifier == identifier)
                        return this[i];
                throw new ArgumentException("Kein IToolStripMenu mit diesem identifier gefunden.");
            }
        }

        #endregion

        public new IToolStripItem this[int index]
        {
            get
            {
                return (IToolStripItem)base[index];
            }
        }
    }
}
